﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using System.Web.Security;
using Components.Data;
using System.Data;
using System.Data.Odbc;

namespace Components.SQL
{
    public class ErendoraMembershipUser : MembershipUser
    {
        private Character[] pCharacters = null;
        private int pCharacterCount;

        public ErendoraMembershipUser(string providerName, string name, Object providerUserKey, string email, string passwordQuestion, string comment, bool isApproved, bool isLockedOut, DateTime creationDate, DateTime lastLoginDate, DateTime lastActivityDate, DateTime lastPasswordChangedDate, DateTime lastLockoutDate)
        : base(providerName, name, providerUserKey, email, passwordQuestion, comment, isApproved, isLockedOut, creationDate, lastLoginDate, lastActivityDate, lastPasswordChangedDate, lastLockoutDate)
        {
            pCharacterCount = -1;
            LoadCharacters();
        }

        public int GetCharacterCount()
        {
            if (pCharacterCount != -1)
                return pCharacterCount;
            OdbcConnection conn = new OdbcConnection(Settings.CharacterConnection);
            OdbcCommand cmd = new OdbcCommand("SELECT COUNT(*) FROM CHARACTER_TBL WHERE account = ? AND isblock = 'F'", conn);
            cmd.Parameters.Add("@account", OdbcType.VarChar, 32).Value = UserName;

            try
            {
                conn.Open();

                pCharacterCount = (int)cmd.ExecuteScalar();
            }
            catch (OdbcException e)
            {
                throw e;
            }
            finally
            {
                conn.Close();
            }
            return pCharacterCount;
        }

        private void LoadCharacters()
        {
            if (GetCharacterCount() <= 0)
                return;

            pCharacters = new Character[pCharacterCount];
            
            OdbcConnection conn = new OdbcConnection(Settings.CharacterConnection);
            OdbcCommand cmd = new OdbcCommand("{CALL webLoadCharacters (?)}", conn);

            cmd.Parameters.Add("@account", OdbcType.VarChar, 32).Value = UserName;
            OdbcDataReader reader = null;

            try
            {
                conn.Open();

                reader = cmd.ExecuteReader();

                int Counter = 0;

                while (reader.Read())
                {
                    pCharacters[Counter++] = new Character(reader.GetString(0), reader.GetString(1), reader.GetInt32(2), reader.GetInt32(3), reader.GetInt32(4), reader.GetInt32(5), reader.GetInt32(6), reader.GetInt32(7));
                }
            }
            catch (OdbcException e)
            {
                throw e;
            }
            finally
            {
                if (reader != null)
                    reader.Close();
                conn.Close();
            }
        }

        public Character GetCharacter(int index)
        {
            if (index >= pCharacterCount || index < 0 || pCharacters == null)
                return null;
            return pCharacters[index];
        }
    }
}
